import { define } from 'cydon'
import { EditableTable } from './EditableTable'
import { error } from '../utils'
import { warning } from '@cydon/ui/Message'
import { API } from '../api'

@define('manage-cr')
export class ManageCR extends EditableTable<{
	selected?: boolean
}> {
	/** 接受选中 */
	accept() {
		const items = this.items.filter(x => x.selected)
		if (!items.length)
			warning('未选中任何项目')
		for (const item of items)
			API.updateCR((<any>item).id, 2).then(() => {
					this.update()
				}, error)
	}

	/** 拒绝选中 */
	reject() {
		const items = this.items.filter(x => x.selected)
		if (!items.length)
			warning('未选中任何项目')
		for (const item of items)
			API.updateCR((<any>item).id, 3).then(() => {
					this.update()
				}, error)
	}
}

declare global {
	interface HTMLElementTagNameMap {
		'manage-cr': ManageCR
	}
}
